using FreeSql.DataAnnotations;

namespace SumerCoreDevOps.Domain.Entities;

/// <summary>
/// 角色实体
/// </summary>
[Table(Name = "roles")]
[Index("idx_role_code", "RoleCode", true)] // 唯一索引
public class Role
{
    /// <summary>
    /// 角色 ID
    /// </summary>
    [Column(IsPrimary = true, IsIdentity = true)]
    public long Id { get; set; }

    /// <summary>
    /// 角色代码（唯一标识）
    /// </summary>
    [Column(StringLength = 100, IsNullable = false)]
    public string RoleCode { get; set; } = null!;

    /// <summary>
    /// 角色名称
    /// </summary>
    [Column(StringLength = 200, IsNullable = false)]
    public string RoleName { get; set; } = null!;

    /// <summary>
    /// 角色描述
    /// </summary>
    [Column(StringLength = 1000)]
    public string? Description { get; set; }

    /// <summary>
    /// 是否系统角色（系统角色不可删除）
    /// </summary>
    [Column(IsNullable = false)]
    public bool IsSystem { get; set; } = false;

    /// <summary>
    /// 是否启用
    /// </summary>
    [Column(IsNullable = false)]
    public bool IsEnabled { get; set; } = true;

    /// <summary>
    /// 排序
    /// </summary>
    [Column(IsNullable = false)]
    public int SortOrder { get; set; } = 0;

    /// <summary>
    /// 创建时间
    /// </summary>
    [Column(IsNullable = false)]
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// 创建者
    /// </summary>
    [Column(StringLength = 100)]
    public string? CreatedBy { get; set; }

    /// <summary>
    /// 更新时间
    /// </summary>
    public DateTime? UpdatedAt { get; set; }

    /// <summary>
    /// 更新者
    /// </summary>
    [Column(StringLength = 100)]
    public string? UpdatedBy { get; set; }

    /// <summary>
    /// 角色权限关联
    /// </summary>
    [Navigate(ManyToMany = typeof(RolePermission))]
    public List<Permission> Permissions { get; set; } = new();

    /// <summary>
    /// 拥有该角色的用户
    /// </summary>
    [Navigate(ManyToMany = typeof(UserRole))]
    public List<User> Users { get; set; } = new();
}
